﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO;

namespace GuestList.ViewModels
{
    public class WindowVM : NotifyPropertyChanged
    {
        private Window _window;
        public Window Window
        {
            get { return _window; }
            set
            {
                _window = value;
                if (_window != null)
                {
                    _window.Closed += delegate
                    {
                        _window = null;
                    };
                }
            }
        }

        public WindowVM(Window window)
        {
            Window = window;
            
            if (!File.Exists(UserSettings.Default.SaveLocation))
            {
                string defaultDir = GetDefaultDirectoryPath();
                if (!Directory.Exists(defaultDir))
                {
                    Directory.CreateDirectory(defaultDir);
                }
                UserSettings.Default.SaveLocation = Path.Combine(defaultDir, "MyList.guestlist");
                UserSettings.Default.Save();
            }
        }

        protected string GetDefaultDirectoryPath()
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "GuestList");
        }

        public virtual void Show()
        {
            if (Window == null)
            {
                Window = GetNewWindowInstance();
            }
            Window.DataContext = this;
            Window.Show();
        }

        protected virtual Window GetNewWindowInstance()
        {
            return new Window();
        }

    }
}
